package com.example.test2.view;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.new6.R;
import com.example.test2.retrofit.RetrofitClient;
import com.example.test2.entity.DictResponse;
import com.example.test2.service.DictService;
import com.google.gson.JsonElement;

public class DictActivity extends AppCompatActivity {
    private TextView textViewHanzi;
    private TextView textViewPinyin;
    private TextView textViewBihua;
    private TextView textViewBushou;
    private TextView textViewWords;
    private TextView textViewBasic;
    private TextView textViewDetail;
    private EditText sourceText;
    private Button queryButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dict);

        sourceText = findViewById(R.id.source_text);
        queryButton = findViewById(R.id.query_button);
        textViewHanzi = findViewById(R.id.hanzi);
        textViewPinyin = findViewById(R.id.pinyin);
        textViewBihua = findViewById(R.id.bihua);
        textViewBushou = findViewById(R.id.bushou);
        textViewWords = findViewById(R.id.words);
        textViewBasic = findViewById(R.id.basic_explain);
        textViewDetail = findViewById(R.id.detail_explain);

        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fetchData(sourceText.getText().toString());
            }
        });
    }

    private void fetchData(String textToQuery) {
        DictService service = RetrofitClient.getClient("https://api.oioweb.cn/api/txt/")
                .create(DictService.class);
        service.getDictData(textToQuery).enqueue(new retrofit2.Callback<DictResponse>() {
            @Override
            public void onResponse(retrofit2.Call<DictResponse> call, retrofit2.Response<DictResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    DictResponse.Result result = response.body().getResult();
                    updateUI(result);
                    if (result.getUrl() != null && !result.getUrl().isEmpty()) {
                        onUrlReceived(result.getUrl());
                    }
                }
            }

            @Override
            public void onFailure(retrofit2.Call<DictResponse> call, Throwable t) {
                // 处理失败情况
            }
        });
    }

    private void updateUI(DictResponse.Result result) {
        runOnUiThread(() -> {
            textViewHanzi.setText("汉字: " + result.getHanzi());
            textViewPinyin.setText("拼音: " + result.getPinyin());
            textViewBihua.setText("笔画: " + result.getBihua());
            textViewBushou.setText("部首: " + result.getBushou());
            textViewWords.setText("组词: " + result.getWords());
            StringBuilder basicBuilder = new StringBuilder();
            for (JsonElement element : result.getBasicExplain()) {
                basicBuilder.append(element.getAsString()).append("\n");
            }
            textViewBasic.setText("基本解释:\n" + basicBuilder.toString());
            StringBuilder detailBuilder = new StringBuilder();
            for (JsonElement element : result.getDetailExplain()) {
                detailBuilder.append(element.getAsString()).append("\n");
            }
            textViewDetail.setText("详细解释:\n" + detailBuilder.toString());
        });
    }

    private void onUrlReceived(String url) {
        runOnUiThread(() -> {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
        });
    }
}
